﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IdeaNMR.Web.Core;
using System.Text.RegularExpressions;

namespace IdeaNMR.Web
{
    public partial class SamplesTable : ListHardObjects_BaseTable<Sample>
    {
        protected override Item[] LoadRawData()
        {
            IdeaNMRdbContainer inmr = new IdeaNMRdbContainer();
            var result = from s in inmr.BaseObjects.OfType<Sample>()
                         where s.User.Username == ScrewTurnWikiFacade.Username || s.IsPrivate == false
                         select new Item
                         {
                             Obj = s
                         };
            return result.ToArray();
        }

        protected override Item[] FilterRawData()
        {
            IEnumerable<Item> result = base.FilterRawData();

            if (this.SearchTerms.Length > 0)
            {
                var res1 = from r in this.RawData
                           where
                              r.Obj.Buffer.ToLowerInvariant().Split(' ').Any(l => Regex.IsMatch(l, this.SearchRegEx)) ||
                              r.Obj.Labeling.ToLowerInvariant().Split(' ').Any(l => Regex.IsMatch(l, this.SearchRegEx))
                           select r;
                result = result.Union(res1);
            }

            return result.ToArray();
        }
    }
}